package it.polito.let.jhp.types;

import java.io.Serializable;

public class BaseType implements JHPType, Serializable {

	private static final long serialVersionUID = 7087927103646566549L;

	private TypeCode type;

	private TypeAttributes attr;

	public BaseType (TypeCode t) {
		this.type = t;
		attr = new TypeAttributes();
		switch (type) {
		case INTEGER:
			attr.IntegerMax = Integer.MAX_VALUE;
			attr.IntegerMin = Integer.MIN_VALUE;
			break;
		case FLOAT:
			attr.FloatMax = Float.POSITIVE_INFINITY;
			attr.FloatMin = Float.NEGATIVE_INFINITY;
			break;
		case STRING:
			attr.StringLenMin = 0;
			attr.StringLenMax = Integer.MAX_VALUE;
			break;
		}
	}

	@Override	
	public boolean typeCheck(Object o, TypeCode t) {
		return type.equals(t);
	}

	@Override		
	public TypeCode getOriginalType() {
		return type;
	}

	@Override	
	public TypeAttributes getAttributes() {
		return attr;
	}

	@Override
	public String clientSideCheck(String value) {
		switch(type) {
		case BOOLEAN:
			return "("+value+" == 'true' || "+value+" == 'false')";
		case INTEGER:
			return "isInteger("+value+")";
		case FLOAT:
			return "isFloat("+value+")";
		case STRING:
			return "true";
		default:
			return "false";
		}

	}

	@Override
	public String toString(){
		return type.toString() + "\r\n";
	}

}
